package com.hnevc.myapplication5;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.hnevc.myapplication5.util.DB;

public class MainActivity extends AppCompatActivity {
    private TextView zhuche;
    private Button login;
    private EditText nameEd;
    private EditText password;
    private int remamberFlag = 0;
    private String password1 = "";
    private CheckBox jizhumm;
    private String usernameStr;
    private String passwordStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SharedPreferences sharedPreferences = getSharedPreferences("test", MODE_PRIVATE);
        initView();
        initEvent();
        if (sharedPreferences != null) {
            String name = sharedPreferences.getString("name", "");
            password1 = sharedPreferences.getString("password", "");
            remamberFlag = sharedPreferences.getInt("remeber_flag", 0);
            nameEd.setText(name);
        }
        if (remamberFlag == 1) {
            jizhumm.setChecked(true);
            password.setText(password1);
        }
    }

    private void initView() {
        zhuche = findViewById(R.id.zhuche_tv);
        login = findViewById(R.id.login_btn);
        nameEd = findViewById(R.id.et_user_name);
        password = findViewById(R.id.et_password);
        jizhumm = findViewById(R.id.jizhu_cb);

    }

    private void initData() {
        usernameStr = nameEd.getText().toString() + "";
        passwordStr = password.getText().toString() + "";

    }

    private void initEvent() {
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
                SharedPreferences sharedPreferences = getSharedPreferences("test", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                DB db = new DB(MainActivity.this);
                db.loginSelect(usernameStr, passwordStr);
                editor.putString("password", passwordStr);
                editor.putString("name", usernameStr);
                if (jizhumm.isChecked()) {
                    remamberFlag = 1;
                    editor.putInt("remeber_flag", remamberFlag);
                    editor.putString("password", passwordStr);
                } else {
                    remamberFlag = 0;
                    editor.putInt("remeber_flag", remamberFlag);
                }
                editor.commit();
                Boolean result = db.loginSelect(usernameStr, passwordStr);
                if (result) {
                    Intent intent = new Intent(MainActivity.this, Main3Activity.class);
                    startActivity(intent);
                    Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        zhuche.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);
            }
        });

    }
}
